package com.cya.lotterysystem.service.activityStatus.operator;

import com.cya.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.cya.lotterysystem.service.dto.ActivityPrizeDO;
import com.cya.lotterysystem.service.dto.ConverActivityStatusDTO;
import com.cya.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/11/7 11:58
 * @description：
 * @modified By：
 * @version:
 */
@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConverActivityStatusDTO converActivityStatusDTO) {
        Long activityId=converActivityStatusDTO.getActivityId();
        Long prizeId= converActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus=converActivityStatusDTO.getTargetPrizeStatus();
        if (null==prizeId||null==targetStatus){
            return false;
        }

        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);

        if (null==activityPrizeDO){
            return false;
        }

        //判断当前奖品状态和目标状态是否一致
        if (targetStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())){
            return false;
        }

        return true;
    }

    @Override
    public Boolean convert(ConverActivityStatusDTO converActivityStatusDTO) {
        Long activityId=converActivityStatusDTO.getActivityId();
        Long prizeId= converActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus=converActivityStatusDTO.getTargetPrizeStatus();
       try {
           activityPrizeMapper.updateStatus(activityId,prizeId,targetStatus.name());
           return true;

       }catch (Exception e){
           return false;
       }
    }
}